package edu.uit.se.laps.web.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import edu.uit.se.laps.web.model.TaiKhoan;
import edu.uit.se.laps.web.util.HibernateUtil;

public class TaiKhoanManager extends HibernateUtil{

	public TaiKhoan add(TaiKhoan taiKhoan){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(taiKhoan);
		session.getTransaction().commit();
		return taiKhoan;
	}
	
	public TaiKhoan getUser(int idUser){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			String sql = "from TaiKhoan where idUser = ?";
			Query query = session.createQuery(sql);
			query.setInteger(0, idUser);
			
			
			TaiKhoan taiKhoan = (TaiKhoan)query.uniqueResult();
			session.getTransaction().commit();
			
			return taiKhoan;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			System.out.println("Loi login");
		}
			
		return null;
	}
	
	
	public TaiKhoan getUser(String email){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			String sql = "from TaiKhoan where email = ?";
			Query query = session.createQuery(sql);
			query.setString(0, email);
			
			
			TaiKhoan taiKhoan = (TaiKhoan)query.uniqueResult();
			session.getTransaction().commit();
			
			return taiKhoan;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			System.out.println("Loi login");
		}
			
		return null;
	}
	
	
	
	public TaiKhoan getUser(String email, String password){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			String sql = "from TaiKhoan where email = ? and password = ?";
			Query query = session.createQuery(sql);
			query.setString(0, email);
			query.setString(1, password);
			
			TaiKhoan taiKhoan = (TaiKhoan)query.uniqueResult();
			session.getTransaction().commit();
			
			return taiKhoan;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			System.out.println("Loi login");
		}
			
		return null;
	}	
	
	public TaiKhoan delete(int idUser){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		TaiKhoan taiKhoan = (TaiKhoan)session.get(TaiKhoan.class, idUser);
		if (null != taiKhoan){
			session.delete(taiKhoan);
		}
		session.getTransaction().commit();
		return taiKhoan;
	}	
	public List<TaiKhoan> list(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<TaiKhoan> taiKhoans = null;
		try {
			taiKhoans = (List<TaiKhoan>)session.createQuery("from TaiKhoan").list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return taiKhoans;
	}
	
	public boolean update(TaiKhoan taiKhoan){
		boolean result = false;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.update(taiKhoan);
			result = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
		}		
		session.getTransaction().commit();
		return result;
	}
}
